import { CanvasContext } from "./designType";
import { Point } from "./Point";

export class Shape {
  public points: Point[] = [];
  public scale: number = 1;
  public origin: Point = new Point(0, 0);

  constructor() {}

    // 从 JSON 文件加载形状数据并更新当前实例的 points
    async load(filePath: string): Promise<void> {
      const fs = wx.getFileSystemManager();
      return new Promise((resolve, reject) => {
        fs.readFile({
          filePath,
          encoding: 'utf-8',
          success: (res) => {
            const data = JSON.parse(res.data as string);
            this.points = data.points;
            resolve();
          },
          fail: (err) => {
            console.error("读取文件失败:", err);
            reject(err);
          }
        });
      });
    }
  
  setScale(scale: number): void {
    this.scale = scale;
  }
  pushPoints(points: Point[]) {
    this.points.push(...points);
  }

  setStartPoint(origin: Point) {
    this.origin = origin;
  }

  // 绘制形状路径
  draw(ctx: CanvasContext): void {    
    if (this.points.length > 0) {
      for (const point of this.points) {
        point.draw(ctx, this.origin, this.scale);
      }
    }
  }
}
